home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / W7JCXF (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  1.6 KB  |  72 lines

  1. package java.io;
  2.  
  3. public class ByteArrayInputStream extends InputStream {
  4.    protected byte[] buf;
  5.    protected int pos;
  6.    protected int mark = 0;
  7.    protected int count;
  8.  
  9.    public ByteArrayInputStream(byte[] buf) {
  10.       this.buf = buf;
  11.       this.pos = 0;
  12.       this.count = buf.length;
  13.    }
  14.  
  15.    public ByteArrayInputStream(byte[] buf, int offset, int length) {
  16.       this.buf = buf;
  17.       this.pos = offset;
  18.       this.count = Math.min(offset + length, buf.length);
  19.    }
  20.  
  21.    public synchronized int available() {
  22.       return this.count - this.pos;
  23.    }
  24.  
  25.    public void mark(int markpos) {
  26.       this.mark = this.pos;
  27.    }
  28.  
  29.    public boolean markSupported() {
  30.       return true;
  31.    }
  32.  
  33.    public synchronized int read() {
  34.       return this.pos < this.count ? this.buf[this.pos++] & 255 : -1;
  35.    }
  36.  
  37.    public synchronized int read(byte[] b, int off, int len) {
  38.       if (this.pos >= this.count) {
  39.          return -1;
  40.       } else {
  41.          if (this.pos + len > this.count) {
  42.             len = this.count - this.pos;
  43.          }
  44.  
  45.          if (len <= 0) {
  46.             return 0;
  47.          } else {
  48.             System.arraycopy(this.buf, this.pos, b, off, len);
  49.             this.pos += len;
  50.             return len;
  51.          }
  52.       }
  53.    }
  54.  
  55.    public synchronized void reset() {
  56.       this.pos = this.mark;
  57.    }
  58.  
  59.    public synchronized long skip(long n) {
  60.       if ((long)this.pos + n > (long)this.count) {
  61.          n = (long)(this.count - this.pos);
  62.       }
  63.  
  64.       if (n < 0L) {
  65.          return 0L;
  66.       } else {
  67.          this.pos = (int)((long)this.pos + n);
  68.          return n;
  69.       }
  70.    }
  71. }
  72.